/*
 * Project name:
     ADC on LCD (Display the result of ADC on Lcd display)
 * Revision History:
       - initial release; From Mikroe forum;
 * Description:
      A simple example of using the ADC library.
      ADC results are displayed on LCD
 * Test configuration:
     MCU:             PIC16F887
     Dev.Board:       EasyPIC6
     Oscillator:      HS, 08.0000 MHz
     Ext. Modules:    LCD module
     SW:              mikroC PRO for PIC
 * NOTES:
     - To simulate analog input on ADC channel 2, use on-board potentiometer P1
       by connecting jumper J15 to MCU pin corresponding to ADC channel 2 input.
 */


unsigned char ch;
unsigned int adc_rd;
char *text;
long tlong;

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

void main() {

  ANSEL  = 0x04;                           // Configure AN2 pin as analog
  ANSELH = 0;                              // Configure other AN pins as digital I/O
  C1ON_bit = 0;                            // Disable comparators
  C2ON_bit = 0;

  TRISA  = 0xFF;                           // PORTA is input

  Lcd_Init();
  LCD_Cmd(_LCD_CURSOR_OFF);                // send command to LCD (cursor off)
  LCD_Cmd(_LCD_CLEAR);                     // send command  to LCD (clear LCD)

  text = "mikroElektronika";               // assign text to string
  LCD_Out(1,1,text);                       // print string a on LCD, 1st row, 1st column
  text = "ADC on LCD";                    // assign text to string
  LCD_Out(2,1,text);                       // print string a on LCD, 2nd row, 1st column

  Delay_ms(2000);
  text  = "voltage:";                      // assign text to string
  while (1) {
    adc_rd  = ADC_read(2);                 // get ADC value from 2nd channel
    LCD_Out(2,1,text);                     // print string a on LCD, 2nd row, 1st column

    tlong = (long)adc_rd * 5000;           // convert adc reading to milivolts
    tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

    ch = tlong / 1000;                     // extract volts digit
    LCD_Chr(2,9,48+ch);                    // write ASCII digit at 2nd row, 9th column
    LCD_Chr_CP('.');

    ch = (tlong / 100) % 10;               // extract 0.1 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch = (tlong / 10) % 10;                // extract 0.01 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch = tlong % 10;                       // extract 0.001 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point
    LCD_Chr_CP('V');

    Delay_ms(1);
  }
}
